<?php
/**
 * contact前台页面
 * author wang
 */
define("PHP_IN",true);
require "include/init.php";
require ROOT_PATH."smarty_connection.php";

$sql = "SELECT * FROM article WHERE articleid = 2";   //联系我们
$result = $db->query($sql);
$article = $db->fetch_one($result);

if(!empty($_POST) && $_POST['dosubmit']){
    $username = $_POST['username'];
    $mail = $_POST['mail'];
    $phone = $_POST['phone'];
    $text_content = $_POST['text_content'];
    $t = time();
    if($username==""){
        alert("用户名不能为空");
        exit;
    }
    if(strlen($username)<4 || strlen($username)>15){
        alert("用户名长度为4-15位");
        exit;
    }
    $match = preg_match("/^[a-z][a-z0-9]{4,14}$/i",$username);
    if(!$match){
        alert("用户名只能为英文字母开始的英文字母、数字任意组合");
        exit;
    }
    if(!is_phone($phone)){
        alert("手机号码格式不正确，请重新输入");
        exit;
    }
    if(!is_email($mail)){
        alert("电子邮箱格式不正确，请重新输入");
        exit;
    }
    if($text_content==""){
        alert("用户名不能为空");
        exit;
    }
    if(strlen($text_content)<10){
        alert("留言内容不少于10个字符");
        exit;
    }
    $sql = "INSERT INTO guestbook (username,email,phone,content,addtime) VALUES ('$username','$mail','$phone','$text_content','$t')";
    $result = $db->query($sql);
    if($result){
        alert("留言成功","contact.php");
    }else{
        alert("留言失败","contact.php");
    }
}

$smarty->assign("article",$article);

$smarty->display("contact.tpl");
$db->close();
?>